/**
 * Created by 1 on 2016/12/31.
 */
(function () {
    "use strict";
    function Tetris() {
        this._sound;
        this._state = "playing";
        this.nextShape = new window.NextShape();
        this.self = new window.Board(this);
        this.timer = new Timer();
        (new window.Keyboard())._init(this.self);
    }

    Tetris.prototype = {
        constructor: Tetris,
        _initAudio: function () {
            this._sound = new Howl({
                src: ["media/bg.wav","media/bg.mp3"],
                loop: true,
                volume: 0.3
            });
            if (window.TetrisConfig.enable) {
                this._sound.play();
            }
        },
        _startTick: function () {
            var self = this.self;
            window.Intervalid = window.setInterval(function () {
                self.tick();
            }, window.TetrisConfig.speed);
        },
        _stopTick: function () {
            window.clearInterval(window.Intervalid);
        },
        startGame: function () {
            this._startTick();
        },
        endGame: function () {
            //停止声音播放
            this._sound.stop();
            //停止tick
            this._stopTick();
            //停止计时
            this.timer.stop();
        },
        pause: function () {
            if (this._state === "over") {
                return;
            }
            //暂停声音播放
            this._sound.pause();
            //停止监听键盘事件
            this._state = "pause";
            //停止tick
            this._stopTick();
            //暂停计时
            this.timer.pause();
        },
        resume: function () {
            if (this._state === "over") {
                return;
            }
            if (window.TetrisConfig.enable) {
                this._sound.play();
            }
            this._state = "playing";
            this._startTick();
            this.timer.resume();
        }
    };
    window.Tetris = Tetris;
})();